<!DOCTYPE html>
<body>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>

var xWidth = xHeight = 10;

function checkImageData(expectedColorSetting, imageData) {
  assert_equals(expectedColorSetting.colorSpace, imageData.colorSpace);
  assert_equals(expectedColorSetting.storageFormat, imageData.storageFormat);
  assert_equals(imageData.data.length, 4 * xWidth * xHeight);
}

function getImageSetting(canvasColorSettings) {
  var imageSetting = {};
  imageSetting.colorSpace = canvasColorSettings.colorSpace;
  if (canvasColorSettings.pixelFormat == "uint8") {
    imageSetting.storageFormat = "uint8";
  } else {
    imageSetting.storageFormat = "float32";
  }
  return imageSetting;
}

// Test createImageData when the canvas is color managed
var helperImageData = new ImageData(xWidth, xHeight);
function runTestCreateImageData(canvasColorSettings) {
  var aCanvas = document.createElement("canvas");
  aCanvas.width = xWidth;
  aCanvas.height = xHeight;
  var ctx = aCanvas.getContext('2d', canvasColorSettings);
  var imageSetting = getImageSetting(canvasColorSettings);
  var imageData = ctx.createImageData(xWidth, xHeight, imageSetting);
  checkImageData(imageSetting, imageData);

  imageData = ctx.createImageData(helperImageData);
  checkImageData(helperImageData.getSettings(), imageData);
}

var testScenariosCreateImageData = [
  ["Test color managed cretateImageData: {srgb, uint8} -> {srgb, uint8}",
      {colorSpace: "srgb", pixelFormat: "uint8"}],
  ["Test color managed cretateImageData: {srgb, float16} -> {srgb, float32}",
      {colorSpace: "srgb", pixelFormat: "float16"}],
  ["Test color managed cretateImageData: {rec2020, float16} -> {rec2020, float32}",
      {colorSpace: "rec2020", pixelFormat: "float16"}],
  ["Test color managed cretateImageData: {p3, float16} -> {p3, float32}",
      {colorSpace: "display-p3", pixelFormat: "float16"}],
];

generate_tests(runTestCreateImageData, testScenariosCreateImageData);

// Test getImageData when the canvas is color managed
function runTestGetImageData(canvasColorSettings) {
  var aCanvas = document.createElement("canvas");
  aCanvas.width = xWidth;
  aCanvas.height = xHeight;
  var ctx = aCanvas.getContext('2d', canvasColorSettings);
  var imageSetting = getImageSetting(canvasColorSettings);
  var imageData = ctx.getImageData(0, 0, xWidth, xHeight, imageSetting);
  checkImageData(imageSetting, imageData);
}

var testScenariosGetImageData = [
  ["Test color managed getImageData: {srgb, uint8} -> {srgb, uint8}",
      {colorSpace: "srgb", pixelFormat: "uint8"}],
  ["Test color managed getImageData: {srgb, float16} -> {srgb, float32}",
      {colorSpace: "srgb", pixelFormat: "float16"}],
  ["Test color managed getImageData: {rec2020, float16} -> {rec2020, float32}",
      {colorSpace: "rec2020", pixelFormat: "float16"}],
  ["Test color managed getImageData: {p3, float16} -> {p3, float32}",
      {colorSpace: "display-p3", pixelFormat: "float16"}],
];

generate_tests(runTestGetImageData, testScenariosGetImageData);

// Test putImageData when the canvas is color managed.
// This test passes if differen possible calls do not crash. To avoid hard
// coded expected results that need to get synced after related Skia updates,
// the output correcteness is verified as a unit test. Please see:
// CanvasRenderingContext2DTest::ColorManagedGetPutImageData


var testScenariosPutImageData = [];
function prepareTestScenariosPutImageData() {
  var colorSpaces = ["srgb", "display-p3", "rec2020"];
  var imageDataStorageFormats = ["uint8", "uint16", "float32"];
  var canvasPixelFormats = ["uint8", "float16"];

  for (i = 0; i < colorSpaces.length; i++)
    for (j = 0; j < imageDataStorageFormats.length; j++)
      for (k = 0; k < colorSpaces.length; k++)
        for (l = 0; l < canvasPixelFormats.length; l++) {
          testTitle = "Test color managed putImageData: ".concat(
            "{", colorSpaces[i], ", ", imageDataStorageFormats[j], "} -> {", colorSpaces[k],
            ", ", canvasPixelFormats[l], "}");
          imageDataColorSettings =
            {colorSpace: colorSpaces[i], storageFormat: imageDataStorageFormats[j]};
          canvasColorSettings =
            {colorSpace: colorSpaces[k], pixelFormat: canvasPixelFormats[l]};
          testScenariosPutImageData.push([testTitle, imageDataColorSettings, canvasColorSettings]);
        }
}

function createAndPutImageData(imageDataColorSettings, canvasColorSettings) {
  // create color managed canvas
  var aCanvas = document.createElement("canvas");
  aCanvas.width = xWidth;
  aCanvas.height = xHeight;
  var ctx = aCanvas.getContext('2d', canvasColorSettings);
  // create color managed ImageData
  var imageData = ctx.createImageData(xWidth, xHeight, imageDataColorSettings);
  // This test does not check the values that were written.
  ctx.putImageData(imageData, 0, 0);
}

function runTestPutImageData(imageDataColorSettings, canvasColorSettings) {
  createAndPutImageData(imageDataColorSettings, canvasColorSettings);
  createAndPutImageData(imageDataColorSettings, canvasColorSettings);
  createAndPutImageData(imageDataColorSettings, canvasColorSettings);
}

prepareTestScenariosPutImageData();
generate_tests(runTestPutImageData, testScenariosPutImageData);

</script>
</body>
